import pymysql
import re

conn = pymysql.connect(host='gsy-cloud', user='root', password='rootQWE', database='python', port=32769)
cursor = conn.cursor()
query_sql = "select comment from tb_rate_comment"

cursor.execute(query_sql)
rows = cursor.fetchall()
word_count = {}
for row in rows:
    comment = str(row[0])
    comment = re.split(r'\s+', re.sub(re.compile(r"[.\"0-9,=%\-()]"), " ", comment))
    for i in comment:
        if i:
            word_count[i] = word_count.get(i, 0) + 1

ll = sorted(word_count.items(), key = lambda kv:(kv[1], kv[0]))
ll.reverse()
# print(ll)
with open('stopcount.csv', 'w', encoding='utf-8') as f:
    for i in ll:
        f.write(i[0] + ',' +str(i[1]) + '\n')
